var baseUrl;

$('document').ready(function() {

    baseUrl = $('#baseUrl').val();
           
    $("#userDetails").tabs({
    	cache:true,
    	load: function(event, ui) {
    		$.getScript(baseUrl + '/public/scripts/tab/' + $(ui.tab).attr('title') + '/index.js', function() {   		    
                
                $('input[type=submit], input[type=button], input[type=reset], button').addClass('ui-state-default ui-corner-all');

                // adds hover class to elements with the class state-default
                $('a.ui-state-default, input.ui-state-default, button.ui-state-default').hover(
                    function(){ $(this).addClass('ui-state-hover'); }, 
                    function(){ $(this).removeClass('ui-state-hover'); }
                );                
    		});
    	}
    });
    
    $('#editDialog').dialog({ 
        modal: true, 
        autoOpen: false,
        resizable: false,
        overlay: { 
            opacity: 0.5, 
            background: "black" 
        }, 
        width: 600,
        height: 400,
        close: function(event, ui) {
            $('#editDialog').html('<div id="modalLoading"></div>');
        }
    }, "close");    
    
    $('a.modalLink').live('click', function(e) {
        e.preventDefault();
        showModal($(this).attr('href'));
    });
});

function showModal(href) {
    $('#editDialog').dialog('open');
    $('#modalLoading').css('display', 'block');
                    
    $.ajax({
        type: "get",
        url: href,
        success: function(data){
            $('#editDialog').html(data);
            $('button#cancel').click(function(e) {
                $('#editDialog').dialog('close');
                $('#problemId').val(originalProblemId);
            });
                            
            $('input[type=submit], input[type=button], input[type=reset], button').addClass('ui-state-default ui-corner-all');

            // adds hover class to elements with the class state-default
            $('a.ui-state-default, input.ui-state-default, button.ui-state-default').hover(
                function(){ $(this).addClass('ui-state-hover'); }, 
                function(){ $(this).removeClass('ui-state-hover'); }
            );              
        },
        error: function(msg) {
            $('#editDialog').dialog('close');
            $('#problemId').val(originalProblemId);
            alert(msg);
        }
    });        
}